var oDay = new Date();
var nowyear = oDay.getYear(); //当前年份
var nowfullyear = oDay.getFullYear(); //完整的年月日（xx年，xx月，xx日）
var nowmoth = oDay.getMonth(); //当前的月份(0-11,0代表1月)         // 获取当前的月份是oDay.getMonth()+1; 
var nowDate = oDay.getDate(); //当前的日(1-31) 
var nowDay = oDay.getDay(); //当前的星期X(0-6,0代表星期天) 
var nowTime = oDay.getTime(); //当前的时间(从1970.1.1开始的毫秒数) 
var nowHours = oDay.getHours(); //当前的小时数(0-23) 
var nowMinutes = oDay.getMinutes(); //当前的分钟数(0-59) 
var nowSeconds = oDay.getSeconds(); //当前的秒数(0-59) 
var noMilliseconds = oDay.getMilliseconds(); //当前的毫秒数(0-999) 
var nowDateString = oDay.toLocaleDateString(); //当前的日期 
var oTime = oDay.toLocaleTimeString(); //当前的时间  <br>oDay.toLocaleString( ); //日期与时间  
var isclick;
var isruntime = false;

$(function() {


    var xhh = '<div class="showcalen"></div><div class="xhcalened"><div class="xhruntime"><span></span></div><div class="clearfix"></div><div class="xhmenu"><a class="fullleft prev">«</a><label></label><a class="fullright next">»</a><div class="clearfix"></div></div><div class="xhcalen_top"><ul><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul></div><div class="xhcanlendate"></div></div>';
    var isloadrl = $('#yecalend').length;
    if (isloadrl > 0) {
        isruntime = $('#yecalend').attr('isruntime');
        isruntime = isruntime == 'true' ? true : false;
        $('#yecalend').css('position', 'relative');
        $('#yecalend').html(xhh);
        startcalen();
    }

    RunningTime();
    maxDay = getMonthDay(nowfullyear, nowmoth + 1);

    $('.xhmenu .prev').click(function() {
        var xhnowdate = $('.xhmenu').find('label').text();
        var xhyear = xhnowdate.substring(0, 4);
        var xhmoth = xhnowdate.substring(5, 7);
        xhmoth = xhmoth - 1;
        if (xhmoth <= 0) {
            xhyear = xhyear - 1;
            xhmoth = 12;
        }
        let xhday = new Date(xhyear, xhmoth * 1 - 1, 1).getDay();
        startcalen(xhyear, xhmoth, xhday);
    });
    $('.xhmenu .next').click(function() {
        var xhnowdate = $('.xhmenu').find('label').text();
        var xhyear = xhnowdate.substring(0, 4);
        var xhmoth = xhnowdate.substring(5, 7);
        xhmoth = xhmoth * 1 + 1;
        if (xhmoth > 12) {
            xhyear = xhyear * 1 + 1;
            xhmoth = 1;
        }
        let xhday = new Date(xhyear, xhmoth * 1 - 1, 1).getDay();
        startcalen(xhyear, xhmoth, xhday);
    });

    $(".showcalen").on("click", function(e) {
        $(document).one("click", function() {
            $(".xhcalened").hide(400);
        });
        e.stopPropagation();
    });

    $('.xhcalened').on("click", function(e) {
        e.stopPropagation();
    })

    $('.showcalen').click(function() {
        var isdisplaynone = $('.xhcalened').css('display');
        if (isdisplaynone == 'none') {
            $('.xhcalened').show(400);
        } else {
            $(".xhcalened").hide(400);
        }
    });


})

// 获取该月的天数
function getMonthDay(year, month) {
    let days = new Date(year, month, 0).getDate();
    return days;
}

function startcalen(year = nowfullyear, moth = nowmoth + 1, isday = 'null', xhdate = nowDate) {
    var h = '';
    maxDay = getMonthDay(year, moth); // 算出当前的月份总共有多少天
    oDay.setDate(1);
    var nowmothfirst = oDay.getDay(); // 该月的第一天是星期几
    nowmothfirst = isday == 'null' ? nowmothfirst : isday;
    lastmaxDay = getMonthDay(year, moth - 1); // 该月上个月的天数
    nextmaxDay = getMonthDay(year, moth + 1); // 该月下个月的天数

    // 循环该月的日期
    h += '<ul>';
    for (var i = 1; i <= maxDay; i++) {
        let exverday = new Date(year, moth * 1 - 1, i).getDay();
        var xhactive = i == xhdate ? "xhactive" : "";
        var xiu = '';
        xiu = exverday == 0 || (moth == 10 && i <= 8) || (moth == 1 && i <= 3) || (moth == 6 && i == 1) ? '<i>休</i>' : '';
        var holiday = '';
        moth == 10 && i == 1 ? holiday = '<b>国庆节</b>' : '';
        moth == 1 && i == 1 ? holiday = '<b>元旦节</b>' : '';
        moth == 2 && i == 14 ? holiday = '<b>情人节</b>' : '';
        moth == 6 && i == 1 ? holiday = '<b>儿童节</b>' : '';
        h += '<li class="' + xhactive + '" onclick="nowdate(this)">' + xiu + '<span>' + i + '</span>' + holiday + '</li>';
    }
    h += '</ul>';
    $('.xhcanlendate').html(h);

    var lh = '';
    // 循环出该月显示上个月的剩下的天数
    for (var i = lastmaxDay - nowmothfirst + 1; i <= lastmaxDay; i++) {
        lh += '<li class="xhelseday">' + i + '</li>';
    }
    $('.xhcanlendate').find('ul').prepend(lh);

    var nh = '';
    // 循环出该月显示下个月的剩下的天数
    for (var i = 1; i <= (42 - maxDay - nowmothfirst); i++) {
        nh += '<li class="xhelseday">' + i + '</li>';
    }
    $('.xhcanlendate').find('ul').append(nh);
    var clickday = new Date(year, moth * 1 - 1, xhdate).getDay();
    var products = [{ 0: '日', 1: '一', 2: '二', 3: '三', 4: '四', 5: '五', 6: '六' }];
    moth < 10 ? moth = '0' + moth : '';
    xhdate < 10 ? xhdate = '0' + xhdate : '';
    var xhnowdate = year + '年' + moth + '月' + xhdate + '日' + '   星期' + products[0][clickday];
    $('.xhmenu').find('label').text(xhnowdate);
    $('.showcalen').html(xhnowdate);
}

function RunningTime() {
    var nowDate = new Date();
    var h = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
    var m = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
    var s = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
    var nowTime = h + ":" + m + ":" + s;
    $('.xhruntime').find('span').html(nowTime);
    var xhnowdate = $('.xhmenu').find('label').text();
    isruntime == true ? $('.showcalen').html(xhnowdate + '   ' + nowTime) : '';
    setInterval(function() { RunningTime(); }, 1000)
}

function nowdate(obj) {
    var xhdate = $(obj).find('span').text();
    var xhnowdate = $('.xhmenu').find('label').text();
    var xhyear = xhnowdate.substring(0, 4);
    var xhmoth = xhnowdate.substring(5, 7);
    xhmoth = xhmoth * 1 - 1;
    let xhday = new Date(xhyear, xhmoth, 1).getDay();
    xhmoth = xhmoth * 1 + 1;
    startcalen(xhyear, xhmoth, xhday, xhdate);
}